//
//  TouchLayer.cpp
//  Seek&Hide
//
//  Created by 齐思 陈 on 13-10-5.
//
//

#include "TouchLayer.h"

bool HelloWorld::init()
{
	  this->setTouchEnabled(true);

	  //获得地图数据
	  CCNotificationCenter::sharedNotificationCenter()->	addObserver(
			this,
			callfuncO_selector(HelloWorld::getMap),
			"map",
			NULL);


	  //获得当前屏幕右上角相对于大地图的位置(像素）
	  CCNotificationCenter::sharedNotificationCenter()->addObserver(
			this,
			callfuncO_selector(HelloWorld::getMapPoint),
			"map_point",
			NULL);

	  //获得获得主角当前在大地图上的位置（像素）
	  CCNotificationCenter::sharedNotificationCenter()->addObserver(
			this,
			callfuncO_selector(HelloWorld::getPlayerPoint),
			"player_point",
			NULL);

	  return true;
}


void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	  CCTouch *touch = (CCTouch*)pTouches->anyObject();
	  CCPoint point = touch->getLocationInView();
	  CCPoint curPoint =convertToMapPoint(point);

	  bool judge = false;
	  for( int i = 0 ; i <mp_runnable_block->count() ;i++)
	  {
			if(curPoint.equals(mp_runnable_block->getControlPointAtIndex(i)))
			{
				  judge = true;
				  break;
			}
	  }
	  if (judge == false)	  return;

	  //主角走到该位置上
	  m_player_point = curPoint;

	  //更新周围可走路径
	  while(mp_runnable_block->count != 0)
	  {
			mp_runnable_block->removeControlPointAtIndex(0);
	  }
	  outputRBlock(m_player_point);

}

//判断该点是否可走，outputRBlock调用
bool HelloWorld::touchTest(CCPoint point)
{	  

	  int tiledGid = meta->tileGIDAt(point);
	  if (tiledGid!= 0)
	  {
			CCDictionary *p = map->propertiesForGID(tiledGid);
			const CCString *prop = p->valueForKey("Collidable");

			if (prop->m_sString.compare("true")==0)
			{
				  meta->removeTileAt(point);
			}
	  }
}

//屏幕点转换为大地图坐标函数
CCPoint HelloWorld::convertToMapPoint(CCPoint trans_point)
{
	  CCPoint truePoint =convertToWorldSpace(trans_point);

	  truePoint.x += m_map_point.x;
	  truePoint.y += m_map_point.y;

	  int x = truePoint.x/32;
	  int y = truePoint.y/32;
	  CCPoint curPoint = ccp(x,y);


	  return curPoint;
}

//输入一个点判断周围可走点函数
void HelloWorld::outputRBlock(CCPoint point)
{
	  if(point.x >0)
	  {
			if (touchTest(ccp(point.x-1 ,point.y)))
			{
				  mp_runnable_block->addControlPoint (ccp(point.x-1 ,point.y));
			}
			
	  }
	  if(point.x <100)
	  {
			if (touchTest(ccp(point.x+1 ,point.y)))
			{
				  mp_runnable_block->addControlPoint (ccp(point.x+1 ,point.y));
			}
	  }
	  if(point.y >0)
	  {
			if (touchTest(ccp(point.x ,point.y-1)))
			{
				  mp_runnable_block->addControlPoint (ccp(point.x ,point.y-1));
			}
	  }
	  if(point.y <100)
	  {
			if (touchTest(ccp(point.x ,point.y+1)))
			{
				  mp_runnable_block->addControlPoint (ccp(point.x ,point.y+1));
			}
	  }

	  //发送通知
	  CCNotificationCenter::sharedNotificationCenter()->postNotification("runnable_block",mp_runnable_block);

}

//大地图坐标转换为屏幕坐标
CCPoint HelloWorld::convertToScreenPoint(CCPoint point)
{
	   
	  float t_x = point.x;
	  float t_y = point.y;

	  t_x = t_x*32 + 16;
	  t_y = t_y*32 + 16;


	  t_x -= m_map_point.x;
	  t_y -= m_map_point.y;

	  t_y = 320 - t_y;

	  return ccp(t_x,t_y);
}

//获得地图
void HelloWorld::getMap(CCTMXTiledMap* m_map)
{
	  this->m_map = m_map;
}

//获得当前屏幕右上角相对于大地图的位置(像素）
void HelloWorld::getMapPoint(CCPoint m_map_point)
{
	  this->m_map_point = m_map_point;
}

//获得主角当前在大地图上的位置（像素）
void HelloWorld::getPlayerPoint(CCPoint m_player_point)
{
	  this->m_player_point = m_player_point;
}

